package com.ynrcc.util;

import java.io.File;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ImportSw {

	public static boolean importSw(String basedir, String sourceId, String brchcd) {
		String content = "";
		String path = basedir + sourceId + "\\sw.txt";
		// 文件不存在则返回
		File file = new File(path);
		if (!file.exists()) {
			return true;
		}

		content = ReadFile.readFile(path);

		// 判断记录是否已经存在
		String sql = "select * from receive where source_id = '" + sourceId + "'";
		List<Record> receives = Db.find(sql);
		if (receives.size() > 0) {
			System.out.println("记录已存在，导入失败");
			return false;
		}

		if (content.trim().length() == 0) {
			System.out.println("文件读取失败");
			return false;
		} else {
			System.out.println("文件读取成功，文件长度[" + content.trim().length() + "]");
		}

		// 中文名称替换为字段名称
		content = content.replaceFirst("收文类型", "RECEIVE_TYPE");
		content = content.replaceFirst("类别", "TYPE");
		content = content.replaceFirst("收文文号", "RECEIVE_NUM");
		content = content.replaceFirst("收文日期", "RECEIVE_DATE");
		content = content.replaceFirst("来文单位", "FROM_DEPT");
		content = content.replaceFirst("来文文号", "FROM_NUM");
		content = content.replaceFirst("收文人", "RECEIVER");
		content = content.replaceFirst("紧急程度", "PRIORITY");
		content = content.replaceFirst("正文标题", "TITLE");
		content = content.replaceFirst("拟办意见", "PLAN");
		content = content.replaceFirst("领导批示", "LEADER");
		content = content.replaceFirst("部门意见", "DEPARTMENT");
		content = content.replaceFirst("其他意见", "OTHER");
		content = content.replaceFirst("办理结果", "RESULT");
		content = content.replaceFirst("其他部门意见", "DEPARTMENT_OTHER");
		content = content.replaceFirst("流转记录", "FLOW");

		System.out.println(content);
		// 数据库操作
		// 取主键
		sql = "select SEQ_RECEIVE.nextval SEQ from dual";
		int seq = Db.findFirst(sql).getInt("SEQ");

		// 取档案主文档ID
		sql = "select ID FILEID from DOC where SOURCE_ID = '" + sourceId + "'";
		List<Record> rec = Db.find(sql);
		if (rec.size() == 0) {
			System.out.println("档案主文档ID不存在，导入失败");
			return false;
		}
		int fileid = rec.get(0).getInt("FILEID");

		Record receive = new Record().set("ID", seq).set("BRCHCD", brchcd);
		receive.set("FILE_ID", fileid);
		receive.set("SOURCE_ID", sourceId);

		String[] column = content.split("●");
		for (String pair : column) {
			int pos = pair.lastIndexOf("■");
			String name = pair.substring(0, pos);
			String value = pair.substring(pos + 1);

			if (name.equals("PLAN")) {
				String temp = "";
				Document doc = Jsoup.parse(value);
				Elements tables = doc.getElementsByTag("table");
				for (Element table : tables) {
					Elements tds = table.getElementsByTag("td");
					for (Element td : tds) {
						temp = temp + td.text() + "\n";
					}
					temp = temp + "\n";
				}
				value = temp;
			}

			if (name.equals("LEADER")) {
				String temp = "";
				Document doc = Jsoup.parse(value);
				Elements tables = doc.getElementsByTag("table");
				for (Element table : tables) {
					Elements tds = table.getElementsByTag("td");
					for (Element td : tds) {
						temp = temp + td.text() + "\n";
					}
					temp = temp + "\n";
				}
				value = temp;
			}

			if (name.equals("DEPARTMENT")) {
				String temp = "";
				Document doc = Jsoup.parse(value);
				Elements tables = doc.getElementsByTag("table");
				for (Element table : tables) {
					Elements tds = table.getElementsByTag("td");
					for (Element td : tds) {
						temp = temp + td.text() + "\n";
					}
					temp = temp + "\n";
				}
				value = temp;
			}

			if (name.equals("OTHER")) {
				String temp = "";
				Document doc = Jsoup.parse(value);
				Elements tables = doc.getElementsByTag("table");
				for (Element table : tables) {
					Elements tds = table.getElementsByTag("td");
					for (Element td : tds) {
						temp = temp + td.text() + "\n";
					}
					temp = temp + "\n";
				}
				value = temp;
			}

			if (name.equals("RESULT")) {
				String temp = "";
				Document doc = Jsoup.parse(value);
				Elements tables = doc.getElementsByTag("table");
				for (Element table : tables) {
					Elements tds = table.getElementsByTag("td");
					for (Element td : tds) {
						temp = temp + td.text() + "\n";
					}
					temp = temp + "\n";
				}
				value = temp;
			}

			if (name.equals("DEPARTMENT_OTHER")) {
				String temp = "";
				Document doc = Jsoup.parse(value);
				Elements tables = doc.getElementsByTag("table");
				for (Element table : tables) {
					Elements tds = table.getElementsByTag("td");
					for (Element td : tds) {
						temp = temp + td.text() + "\n";
					}
					temp = temp + "\n";
				}
				value = temp;
			}

			if (name.equals("FLOW")) {
				Document doc = Jsoup.parse(value);
				String temp = doc.text();
				for (int i = 0; i < 50; i++) {
					String src = "\\[" + i + "\\]";
					temp = temp.replaceAll(src, "\n" + "[" + i + "]");
				}
				temp = temp.replaceFirst("\n", "");
				value = temp;
			}
			System.out.println(name + "■" + value);
			receive.set(name, value);
		}

		Db.save("receive", receive);
		return true;
	}
}
